package demo_2;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述：无重复字符的最长子串
 *
 * @author lwj
 */
public class Demo_2 {
    public static void main(String[] args) {
        String motherString = "assbcssassbsscddbssdssccbssdwe";

        Map<Character, Integer> pointMap = new HashMap<Character, Integer>();
        int maxLength = 0;
        int strStart = 0;
        String maxStr = "";
        /* 转换为数组，遍历出每个元素出现的位置，并记录
         * 如果出现过，则判断该串长度大小，进行截取*/
        char[] chars = motherString.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (pointMap.containsKey(aChar)) {
                strStart = Math.max(pointMap.get(aChar) + 1, strStart);
            }
            if (i - strStart >= maxLength) {
                maxLength = i - strStart;
                maxStr = motherString.substring(strStart, i + 1);
            }
            pointMap.put(aChar, i);
        }
        System.out.println("最长字符串: " + maxStr);
        System.out.println("长度为：" + (maxLength + 1));
    }
}